# $LastChangedDate: 2011-02-26 11:35:50 -0500 (Sat, 26 Feb 2011) $
# LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG
# Top-level cmake commands to build a project.
# Notes:
# - See module.txt
# - Scope/context:
#   The top-level project (project.cmake) and modules (module.cmake) have their own scope.
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# System-specific.

INCLUDE( CMakeModules/system.cmake )

#-------------------------------------------------------------------------------
# Configure the type of build (release, debug, etc).
#
# Map -Dval cmdline args (see top-level CMakeLists.txt)
# to the vars CMAKE_BUILD_TYPE and PROJECT_BUILD_TYPE.
#
# PROJECT_BUILD_TYPE is distinct from CMAKE_BUILD_TYPE.
# Reasons are that CMAKE_BUILD_TYPE doesn't define profile builds
# and has values that aren't suited to regex matching.
#
# PROJECT_BUILD_TYPE = Debug | DebugMin
#                    | ProfileNoOpt | ProfileOpt
#                    | Release | ReleaseMin | ReleaseSym (default)
#
# So regex "Release" or "Profile" can match any variant.
#
# This mapping to one variable helps simplify further build configuration done later.

# cmake DEBUG_LEVEL becomes "#define DEBUG 1|2" in C++.
SET( DEBUG_LEVEL "0" )

IF( DEBUGGER )
    IF( NOT DEBUG )  # DEBUGGER implies DEBUG>0
        SET( DEBUG "2" )
    ENDIF()
    MESSAGE( STATUS "Compiling for debugger: DEBUGGER=1 DEBUG=${DEBUG}" )
    SET( PROJECT_BUILD_TYPE_EXTRA_MSG "DEBUGGER=1 ${PROJECT_BUILD_TYPE_EXTRA_MSG}" )
ENDIF()

IF( DEBUG )
    SET( PROJECT_BUILD_TYPE "Debug" )
    SET(   CMAKE_BUILD_TYPE "" )  # empty string, "Debug" would cause -g but DEBUG=2 sets -g3
    SET( DEBUG_LEVEL ${DEBUG} )
ELSEIF( DEBMIN )
    # On Apple Mac, debug symbols produces a huge executable (150MB).
    SET( PROJECT_BUILD_TYPE "DebugMin" )
    SET(   CMAKE_BUILD_TYPE "" )  # empty string, "Debug" would cause -g to be passed to gcc
    SET( DEBUG_LEVEL ${DEBMIN} )
ELSEIF( PROFILE_NO_OPT OR PROFILE )
    SET( PROJECT_BUILD_TYPE "ProfileNoOpt" )
    # no CMAKE_BUILD_TYPE counterpart
ELSEIF( PROFILE_OPT )
    SET( PROJECT_BUILD_TYPE "ProfileOpt" )
    # no CMAKE_BUILD_TYPE counterpart
ELSEIF( RELMIN OR MINREL )
    SET( PROJECT_BUILD_TYPE "ReleaseMin" )  # regex "Release" will match this variant
    SET(   CMAKE_BUILD_TYPE "MinSizeRel" )
ELSEIF( RELSYM )
    SET( PROJECT_BUILD_TYPE "ReleaseSym" )  # regex "Release" will match this variant
    SET(   CMAKE_BUILD_TYPE "RelWithDebInfo" )
ELSE()  # default
    SET( PROJECT_BUILD_TYPE "Release" )
    SET(   CMAKE_BUILD_TYPE "Release" )
ENDIF()

#-------------------------------------------------------------------------------

# PROJECT_BEGIN( projectName )
MACRO( PROJECT_BEGIN )
    SET( PROJECT_NAME ${ARGV0} )
    INCLUDE( CMakeModules/project_begin.cmake )
ENDMACRO()

#-------------------------------------------------------------------------------

# PROJECT_END( void )
MACRO( PROJECT_END )
    INCLUDE( CMakeModules/project_end.cmake )
ENDMACRO()
